package com.afterRain.config;

import java.util.Properties;

import com.afterRain.interceptor.UserInterceptor;
import com.afterRain.model._MappingKit;
import com.afterRain.routes.AfterRoutes;
import com.afterRain.util.ActiveRecordPluginUtil;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

/**
* @author 作者 E-mail:
* @version 创建时间：2017年5月9日 上午11:36:54
* 类说明
*/
public class AllConfig extends JFinalConfig{

	@Override
	public void configConstant(Constants con) {
		con.setDevMode(true);
		con.setViewType(ViewType.FREE_MARKER);
	}

	@Override
	public void configEngine(Engine en) {
	}

	@Override
	public void configHandler(Handlers hd) {
		
	}

	@Override
	public void configInterceptor(Interceptors ic) {
		ic.add(new UserInterceptor());
	}
	
   @Override
   public void afterJFinalStart() {
	   //config ftl file
      Properties p = loadPropertyFile("conf/freemarker.properties");//由于我们用到freemarker，所以在此进行freemarker配置文件的装载  
      FreeMarkerRender.setProperties(p);
    }  

	@Override
	public void configPlugin(Plugins pg) {
		loadPropertyFile("conf/jdbc.properties");
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbc_url"), getProperty("jdbc_username"), getProperty("jdbc_password"));
		pg.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath()+"/mapper");
		ActiveRecordPluginUtil.loading(arp, PathKit.getRootClassPath()+"/mapper");
		_MappingKit.mapping(arp);
		pg.add(arp);
	}

	@Override
	public void configRoute(Routes rt) {
		rt.add(new AfterRoutes());
	}
	
	//open the program(开启程序) 
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8080, "/", 5);
	}
}
